﻿using SoundEngine3.AudioProcessing;
using SoundEngine3.DspPlugins;
using System;
using ZquenceStudio3.Core;
using ZquenceStudio3.DspPlugins;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine;
using ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.Protected;
using ZquenceStudio3.Core.Storage;

namespace ZquenceStudio3Host.DspPlugins.Effects
{
    public class ModulizerEffectDescription : IDspPlugin
    {
        public DspPluginInfo GetDspPluginInfo()
        {
            return new DspPluginInfo("ModulizerEffect", DspType.Effect);
        }

        public BaseDspPluginInstance CreateDsp(ZquenceStudio3Context context, DspPluginInfo info)
        {
            return new ModulizerEffect(context, info);
        }
    }

    class ModulizerEffect : BaseModulizer
    {
        public ModulizerEffect(ZquenceStudio3Context coreContext, DspPluginInfo dspPlugin)
            : base(coreContext, dspPlugin, ModuleManager.ModulizerType.Effect)
        {
        }

        protected override void CreateDefaultBuilder()
        {
            mModuleManager.AudioInput.GuiPosition.X = 100;
            mModuleManager.AudioInput.GuiPosition.Y = 5;
            mModuleManager.AudioInput.GuiPosition.Width = 100;
            mModuleManager.AudioInput.GuiPosition.Height = 1;
            
            mModuleManager.Out.GuiPosition.X = 300 + 200;
            mModuleManager.Out.GuiPosition.Y = 5;
            mModuleManager.Out.GuiPosition.Width = 100;
            mModuleManager.Out.GuiPosition.Height = 1;

            mModuleManager.Out.Left.FromOutput = mModuleManager.AudioInput.Left;
            mModuleManager.Out.Right.FromOutput = mModuleManager.AudioInput.Right;
        }

        public override void Process(BaseAudioBuffer input, BaseAudioBuffer output)
        {
            //mModuleManager.AudioInput.Left.AudioBuffer.SetBuffer(input.Channels[0].Samples);
            //mModuleManager.AudioInput.Right.AudioBuffer.SetBuffer(input.Channels[1].Samples);
            Array.Copy(input.Channels[0].Samples, mModuleManager.AudioInput.Left.AudioBuffer.Buffer, mModuleManager.Out.ProcessedLeft.Length);
            Array.Copy(input.Channels[1].Samples, mModuleManager.AudioInput.Right.AudioBuffer.Buffer, mModuleManager.Out.ProcessedRight.Length);

            mModuleManager.InterateProcess();
            
            Array.Copy(mModuleManager.Out.ProcessedLeft, output.Channels[0].Samples, mModuleManager.Out.ProcessedLeft.Length);
            Array.Copy(mModuleManager.Out.ProcessedRight, output.Channels[1].Samples, mModuleManager.Out.ProcessedRight.Length);
        }
        
        public override BaseAudioBuffer OnProcessOutput()
        {
            throw new NotSupportedException("ProcessOutput metod can not be used for NodesizerEffect.");
        }
        
        protected override void OnUnpacked()
        {

        }
    }
}
